// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      shopCartItemDiscounts:[
      {"chooseDiscountItemDto":null,
      "shopCartItems":[{"prodName":"Apple 14 Pro Max",
      "prodCount":1,
      "pic":"https://img.mall4j.com/2019/04/eaa8c9bd3e7b41eaa310adbde10b6401.jpg",
      "price":10000,
      "productTotalAmount":38.0,
      "prodId":1,
      "skuId":1,
      "skuName":"7天包换",
      "basketId":1,
      "actualTotal":null,
      "discountId":0,
      "shareReduce":null,
      "discounts":[],
      "shopId":1,
      "shopName":"mall4j小店1",
      "oriPrice":0.0,
      "distributionCardNo":null,
      "basketDate":"2023-12-22 09:54:37"}]}
    ],
      totalNum:1,
      isProd:false,
      isProdURL:"../../images/icon/prod-col.jpg"
  },
  backToIndex(){
    wx.navigateBack({
      delta:1,
      url:"../index/index"
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    let eventChannel = this.getOpenerEventChannel()
    eventChannel.on("mydata",function(data) {
      that.data.shopCartItemDiscounts[0].shopCartItems[0].price = data.price
      that.data.shopCartItemDiscounts[0].shopCartItems[0].prodName = data.prodName
      that.data.shopCartItemDiscounts[0].shopCartItems[0].prodCount = 1
      that.data.shopCartItemDiscounts[0].shopCartItems[0].pic = data.pic
      that.data.shopCartItemDiscounts[0].shopCartItems[0].skuName = data.brief
      that.data.shopCartItemDiscounts[0].shopCartItems[0].shopName = data.shopName
      that.data.shopCartItemDiscounts[0].shopCartItems[0].basketDate = that.getNowDate()
      that.data.shopCartItemDiscounts[0].shopCartItems[0].prodId = data.prodId

      that.setData({
        shopCartItemDiscounts : that.data.shopCartItemDiscounts
      })
      let cache = wx.getStorageSync('myProd')
      let history = wx.getStorageSync('myHistory')
      that.addHistory()
      for(let a = 0;a<cache.length;a++){
        if(cache[a].shopCartItems[0].prodId==that.data.shopCartItemDiscounts[0].shopCartItems[0].prodId){
          that.setData({
            isProdURL:"../../images/icon/prod-col-red.png",
            isProd:true
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  /*
  前往购物车
  */
  toBasket(e){
    if(e.target.dataset.tobasket=="true"){
      let a = this.data.shopCartItemDiscounts[0]
      getApp().globalData.datas = a
    }
    wx.switchTab({
      url: "../basket/basket",
    })
  },
  buyNow(){
    let data = this.data.shopCartItemDiscounts

    wx.navigateTo({
      url: '../order-detail/order-detail',
      success:function(res){
        res.eventChannel.emit("dataPoster",data)  //发布事件 dataPoster
      }
    })
  },
  getNowDate(){
    let Now = new Date()
    let y = Now.getFullYear()
    let mon = Now.getMonth()+1
    let d = Now.getDate()
    let h = Now.getHours()
    let m = Now.getMinutes()
    let s = Now.getSeconds()
    return ""+y+"-"+mon+"-"+d+" "+h+":"+m+":"+s
  },
  /*
    添加收藏
  */
  addProd(){
    let prodList = wx.getStorageSync('myProd')
    if(this.data.isProd){ //已收藏的情况
      for(let a=0;a<prodList.length;a++){
        if(prodList[a].shopCartItems[0].prodId==this.data.shopCartItemDiscounts[0].shopCartItems[0].prodId){
          prodList.splice(a,1)  
        }
      }
      this.changeStar(true)
    }
    else{ //未收藏的情况
      console.log(prodList)
      if(prodList.length == 0){
        prodList = [this.data.shopCartItemDiscounts[0]]
      }
      else{
        prodList.push(this.data.shopCartItemDiscounts[0])
      }
      this.changeStar(false)
    }
    wx.setStorageSync('myProd', prodList)
  },
  changeStar(status){
    if(status){
      this.setData({
        isProdURL:"../../images/icon/prod-col.jpg",
        isProd:false
      })
    }
    else{
      this.setData({
        isProdURL:"../../images/icon/prod-col-red.png",
        isProd:true
      })
    }
  },
  /*
    添加历史记录
  */
  addHistory(){
    let cache = wx.getStorageSync('myHistory')
    if(cache.length==0){
      cache = [this.data.shopCartItemDiscounts[0]]
    }
    else{
      cache.push(this.data.shopCartItemDiscounts[0])
    }
    wx.setStorageSync('myHistory', cache)
  }
})